class_name ThrowBomb
extends Node2D


@onready var animation_player: AnimationPlayer = $AnimationPlayer

var sender: Node2D
var item_config: ItemBaseConfig

var dir: Vector2
var speed: float = 200
var velocity: Vector2
var gravity: float = 300

@onready var throw_fire_audio: AudioStreamPlayer2D = $AudioStreamPlayer2D


func _ready() -> void:
	animation_player.play("throwing")
	velocity = dir * speed
	throw_fire_audio.play()
	

func _physics_process(delta: float) -> void:
	velocity.y += gravity * delta
	position += velocity * delta
	
	
func explosion():
	throw_fire_audio.stop()
	
	queue_free()
	
	var inst = (ResourceManager.get_resource("particle_explosion_effect") as PackedScene).instantiate() as ParticleExplosionEffect
	var level = get_tree().current_scene as Level
	level.bullets_parent.add_child(inst)
	inst.global_position = global_position
	inst.sender = sender
	inst.attack = item_config.attack
	inst.critical_rate = item_config.critical_rate
	inst.critical_bonus = item_config.critical_bonus
	inst.knockback = item_config.knockback
	inst.hit_cam_shake_strength = item_config.attack_cam_shake_intensity
	inst.hit_cam_shake_duration = item_config.attack_cam_shake_duration
	
	level.camera.start_shake(item_config.attack_cam_shake_intensity, item_config.attack_cam_shake_duration)


func _on_hit_area_body_entered(body: Node2D) -> void:
	explosion()


func _on_hit_area_area_entered(area: Area2D) -> void:
	explosion()
